package GUI.UI
{
    import GUI.UI.Events.UIEvent;
    import GUI.UI.Events.UIScrollEvent;
    
    import flash.display.DisplayObject;
    import flash.display.Graphics;
    import flash.display.Sprite;

	/**
	* <p>
	* UIWindow class extends UIContainer class, to avoid content to be "accidenatlly" drawn outside the window.
	* Normaly in flash, if a child bigger than a parent is added, the parent size is changed to fit the child.
	* </p>
	* <p>
	* In the UIWindow class, if a child exedes the parent size, the mask hides that content.
	* Because of this desition, whenever we are refering to the "window size", we are really refering to the mask width/height, 
	* and not the real "DisplayObject.width/height", wich contains the logical size (if the child is bigger than the parent, the logical size will get bigger).
	* </p>
	* <p>
	* The window class contains a UIContainer inside, to allow content to be display independently from the window bar/border/buttons or whatever other GUI element inside.
	* You should add content inside this container, and not in the window directly, so avoid UIWindow.addChild, and use UIWindow.contentArea.addChild();
	* </p>
	* 
	* @param sizeX
	* @param sizeY
	* 
	*/
    public class UIWindow extends UIDecorated
    {
        protected var _contentArea:UIContainer;
        
        
        public function UIWindow(baseSprite:Sprite, sizeX:Number, sizeY:Number)
        {
            super(baseSprite);
            
            this.viewRectWidth  = sizeX;
            this.viewRectHeight = sizeY;
        }
        
        public function get contentArea():UIContainer
        {
            return _contentArea;
        }
        
        public function set contentArea(value:UIContainer):void
        {
            if(_contentArea)
            {
                removeChild(_contentArea.baseSprite);
                
                _contentArea.destroy();
                
                _contentArea = null;
            }
            
            _contentArea                = value;
            _contentArea.viewRectWidth  = this.viewRectWidth;
            _contentArea.viewRectHeight = this.viewRectHeight;
        }
        
        public function get marginTop():Number
        {
            return _contentArea.y;
        }

        public function set marginTop(value:Number):void
        {
            _contentArea.y = value;
        }
        
        public function get marginBottom():Number
        {
            return this.viewRectHeight - (_contentArea.y + _contentArea.viewRectHeight);
        }

        public function set marginBottom(value:Number):void
        {
            _contentArea.viewRectHeight = this.viewRectHeight - (marginTop + value);
        }

        public function get marginLeft():Number
        {
            return _contentArea.x;
        }
        
        public function set marginLeft(value:Number):void
        {
            _contentArea.x = value;
        }
        
        public function get marginRight():Number
        {
            return this.viewRectWidth - (_contentArea.x + _contentArea.viewRectWidth);
        }

        public function set marginRight(value:Number):void
        {
            _contentArea.viewRectWidth = this.viewRectWidth - (_contentArea.x + value);
        }
        
        public override function destroy():void
        {
            if(_contentArea)
            {
                _contentArea.destroy();
                _contentArea = null;
            }

            super.destroy();
        }
        
        public function addContent(child:DisplayObject):void
        {
            if(_contentArea)
                _contentArea.baseSprite.addChild(child);
        }
        
        public function get graphicsContent():Graphics
        {
            if(_contentArea)
                return _contentArea.graphics;
            
            return null;
        }
        
        /**
        * will process UIEvents and retranmit them to childs according;
        */
        public override function processUIEvent(e:UIEvent):void
        {
            switch (e.type)
            {
                case UIScrollEvent.SCROLL_MORE:
                case UIScrollEvent.SCROLL_LESS:
                    if(_contentArea)
                        _contentArea.processUIEvent(e);
                break;
                
                default:
                    super.processUIEvent(e);
                break;
            }
            
        }
        
        /**
        * overrides addChild, so the window can listen to child events; 
        */
        public override function addChild(child:UIEntity):void
        {
            ListenUIEventsFrom(child);
            
            super.addChild(child);
        }
        
        public override function removeChild(child:DisplayObject):void
        {
            unListenAllUIEvents();
            
            super.removeChild(child);
        }
    }
}